/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    DiskDetails.cs

Abstract: 

    Form to show the details of each disk for each machine.
     
--*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Visio = Microsoft.Office.Interop.Visio;
using System.Drawing.Drawing2D;
using System.Globalization;

namespace NRTMAddIn
{
    public partial class DiskDetails : Form
    {
        #region variables

        //Stores dataset and datatable reference
        private DataSet dsDriveDetails;
        private DataTable dtTempDiskDetails;

        //View Details Form components
        private System.Windows.Forms.TabPage tabPagei;
        private System.Windows.Forms.Label lblFSi;
        private System.Windows.Forms.Label lblTypei;
        private System.Windows.Forms.Label lb_FreeSpacei;
        private System.Windows.Forms.Label lb_UsedSpacei;
        private System.Windows.Forms.Label lb_totSizeGBi;
        private System.Windows.Forms.Label lb_totSizeBi;
        private System.Windows.Forms.Label lb_Capacityi;
        private System.Windows.Forms.Label lb_freeSpaceGBi;
        private System.Windows.Forms.Label lb_freeSpaceBi;
        private System.Windows.Forms.Label lb_Filei;
        private System.Windows.Forms.Label lb_usedSpaceGBi;
        private System.Windows.Forms.Label lb_usedSpaceBi;
        private System.Windows.Forms.Label lb_Typei;
        private System.Windows.Forms.Button bt_close;
        private System.Windows.Forms.TextBox txtBoxUsedi;
        private System.Windows.Forms.TextBox txtBoxFreei;
        private System.Windows.Forms.PictureBox pb_Imagei;
        private System.Windows.Forms.TabControl tb_Disk_Names;
        private System.Windows.Forms.GroupBox line1;
        private System.Windows.Forms.GroupBox line2;
        private System.Windows.Forms.GroupBox line3;
        private System.Windows.Forms.Label lb_monitorStatus;

        private Document nrtmDocument;

        #endregion

        #region properties
        /// <summary>
        /// Gets or sets the NRTM Document object that this dialog is loaded for.
        /// </summary>
        internal Document NRTMDocument
        {          
            set { nrtmDocument = value; }
        }

        #endregion

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent(string i)
        {
            this.tabPagei = new System.Windows.Forms.TabPage();
            this.txtBoxFreei = new System.Windows.Forms.TextBox();
            this.txtBoxUsedi = new System.Windows.Forms.TextBox();
            this.lb_freeSpaceGBi = new System.Windows.Forms.Label();
            this.lb_freeSpaceBi = new System.Windows.Forms.Label();
            this.lb_Filei = new System.Windows.Forms.Label();
            this.lb_usedSpaceGBi = new System.Windows.Forms.Label();
            this.lb_usedSpaceBi = new System.Windows.Forms.Label();
            this.lb_Typei = new System.Windows.Forms.Label();
            this.lb_totSizeGBi = new System.Windows.Forms.Label();
            this.lb_totSizeBi = new System.Windows.Forms.Label();
            this.lb_Capacityi = new System.Windows.Forms.Label();
            this.lblFSi = new System.Windows.Forms.Label();
            this.lblTypei = new System.Windows.Forms.Label();
            this.lb_FreeSpacei = new System.Windows.Forms.Label();
            this.lb_UsedSpacei = new System.Windows.Forms.Label();
            this.pb_Imagei = new System.Windows.Forms.PictureBox();
            this.line1 = new System.Windows.Forms.GroupBox();
            this.line2 = new System.Windows.Forms.GroupBox();
            this.line3 = new System.Windows.Forms.GroupBox();
            this.tabPagei.SuspendLayout();
            this.SuspendLayout();
            // 
            // tb_Disk_Names
            // 
            this.tb_Disk_Names.Controls.Add(this.tabPagei);
            this.tb_Disk_Names.Location = new System.Drawing.Point(10, 12);
            this.tb_Disk_Names.Name = "tb_Disk_Names";
            this.tb_Disk_Names.SelectedIndex = 0;
            this.tb_Disk_Names.Size = new System.Drawing.Size(356, 330);
            this.tb_Disk_Names.TabIndex = 0;
            // 
            // tabPage
            // 
            this.tabPagei.Controls.Add(this.txtBoxFreei);
            this.tabPagei.Controls.Add(this.txtBoxUsedi);
            this.tabPagei.Controls.Add(this.lb_freeSpaceGBi);
            this.tabPagei.Controls.Add(this.lb_freeSpaceBi);
            this.tabPagei.Controls.Add(this.lb_Filei);
            this.tabPagei.Controls.Add(this.lb_usedSpaceGBi);
            this.tabPagei.Controls.Add(this.lb_usedSpaceBi);
            this.tabPagei.Controls.Add(this.lb_Typei);
            this.tabPagei.Controls.Add(this.lb_totSizeGBi);
            this.tabPagei.Controls.Add(this.lb_totSizeBi);
            this.tabPagei.Controls.Add(this.lb_Capacityi);
            this.tabPagei.Controls.Add(this.lblFSi);
            this.tabPagei.Controls.Add(this.lblTypei);
            this.tabPagei.Controls.Add(this.lb_FreeSpacei);
            this.tabPagei.Controls.Add(this.lb_UsedSpacei);
            this.tabPagei.Controls.Add(this.pb_Imagei);
            this.tabPagei.Controls.Add(this.line1);
            this.tabPagei.Controls.Add(this.line2);
            this.tabPagei.Controls.Add(this.line3);
            this.tabPagei.Location = new System.Drawing.Point(4, 22);
            this.tabPagei.Name = "tabPage" + i;
            this.tabPagei.Padding = new System.Windows.Forms.Padding(3);
            this.tabPagei.Size = new System.Drawing.Size(378, 289);
            this.tabPagei.TabIndex = 0;
            this.tabPagei.UseVisualStyleBackColor = true;
            // 
            // line1
            // 
            this.line1.Location = new System.Drawing.Point(6, 63);
            this.line1.Name = "line1";
            this.line1.Size = new System.Drawing.Size(336, 8);
            this.line1.TabIndex = 0;
            this.line1.TabStop = false;
            // 
            // line2
            // 
            this.line2.Location = new System.Drawing.Point(6, 148);
            this.line2.Name = "line2";
            this.line2.Size = new System.Drawing.Size(336, 8);
            this.line2.TabIndex = 105;
            this.line2.TabStop = false;
            // 
            // line3
            // 
            this.line3.Location = new System.Drawing.Point(6, 194);
            this.line3.Name = "line3";
            this.line3.Size = new System.Drawing.Size(336, 8);
            this.line3.TabIndex = 106;
            this.line3.TabStop = false;
            // 
            // lb_File
            // 
            this.lb_Filei.AutoSize = true;
            this.lb_Filei.Location = new System.Drawing.Point(19, 42);
            this.lb_Filei.Name = "lb_File" + i;
            this.lb_Filei.Size = new System.Drawing.Size(63, 13);
            this.lb_Filei.TabIndex = 12;
            this.lb_Filei.Text = Global.GetResourceString("File_Label");
            // 
            // lblFS
            // 
            this.lblFSi.AutoSize = true;
            this.lblFSi.Location = new System.Drawing.Point(107, 42);
            this.lblFSi.Name = "lblFS" + i;
            this.lblFSi.Size = new System.Drawing.Size(0, 13);
            this.lblFSi.TabIndex = 3;
            // 
            // lb_Type
            // 
            this.lb_Typei.AutoSize = true;
            this.lb_Typei.Location = new System.Drawing.Point(19, 19);
            this.lb_Typei.Name = "lb_Type" + i;
            this.lb_Typei.Size = new System.Drawing.Size(34, 13);
            this.lb_Typei.TabIndex = 9;
            this.lb_Typei.Text = Global.GetResourceString("Type_Label");
            // 
            // lblType
            // 
            this.lblTypei.AutoSize = true;
            this.lblTypei.Location = new System.Drawing.Point(107, 19);
            this.lblTypei.Name = "lblType" + i;
            this.lblTypei.Size = new System.Drawing.Size(0, 13);
            this.lblTypei.TabIndex = 2;
            // 
            // txtBoxFree
            // 
            this.txtBoxFreei.BackColor = System.Drawing.Color.Fuchsia;
            this.txtBoxFreei.Enabled = false;
            this.txtBoxFreei.Location = new System.Drawing.Point(19, 119);
            this.txtBoxFreei.Name = "txtBoxFree" + i;
            this.txtBoxFreei.Size = new System.Drawing.Size(23, 20);
            this.txtBoxFreei.TabIndex = 104;
            // 
            // lb_FreeSpace
            // 
            this.lb_FreeSpacei.AutoSize = true;
            this.lb_FreeSpacei.Location = new System.Drawing.Point(50, 119);
            this.lb_FreeSpacei.Name = "lb_FreeSpace" + i;
            this.lb_FreeSpacei.Size = new System.Drawing.Size(65, 13);
            this.lb_FreeSpacei.TabIndex = 1;
            this.lb_FreeSpacei.Text = Global.GetResourceString("FreeSpace_Label");
            // 
            // lb_freeSpaceGB
            // 
            this.lb_freeSpaceGBi.AutoSize = true;
            this.lb_freeSpaceGBi.Location = new System.Drawing.Point(265, 119);
            this.lb_freeSpaceGBi.Name = "lb_freeSpaceGB" + i;
            this.lb_freeSpaceGBi.Size = new System.Drawing.Size(0, 13);
            this.lb_freeSpaceGBi.TabIndex = 14;
            // 
            // lb_freeSpaceB
            // 
            this.lb_freeSpaceBi.AutoSize = true;
            this.lb_freeSpaceBi.Location = new System.Drawing.Point(155, 119);
            this.lb_freeSpaceBi.Name = "lb_freeSpaceB" + i;
            this.lb_freeSpaceBi.Size = new System.Drawing.Size(0, 13);
            this.lb_freeSpaceBi.TabIndex = 13;
            // 
            // txtBoxUsed
            // 
            this.txtBoxUsedi.BackColor = System.Drawing.Color.RoyalBlue;
            this.txtBoxUsedi.Enabled = false;
            this.txtBoxUsedi.Location = new System.Drawing.Point(19, 90);
            this.txtBoxUsedi.Name = "txtBoxUsed" + i;
            this.txtBoxUsedi.Size = new System.Drawing.Size(23, 20);
            this.txtBoxUsedi.TabIndex = 103;
            // 
            // lb_UsedSpace
            // 
            this.lb_UsedSpacei.AutoSize = true;
            this.lb_UsedSpacei.Location = new System.Drawing.Point(50, 90);
            this.lb_UsedSpacei.Name = "lb_UsedSpace" + i;
            this.lb_UsedSpacei.Size = new System.Drawing.Size(69, 13);
            this.lb_UsedSpacei.TabIndex = 0;
            this.lb_UsedSpacei.Text = Global.GetResourceString("UsedSpace_Label");
            // 
            // lb_usedSpaceGB
            // 
            this.lb_usedSpaceGBi.AutoSize = true;
            this.lb_usedSpaceGBi.Location = new System.Drawing.Point(265, 90);
            this.lb_usedSpaceGBi.Name = "lb_usedSpaceGB" + i;
            this.lb_usedSpaceGBi.Size = new System.Drawing.Size(0, 13);
            this.lb_usedSpaceGBi.TabIndex = 11;
            // 
            // lb_usedSpaceB
            // 
            this.lb_usedSpaceBi.AutoSize = true;
            this.lb_usedSpaceBi.Location = new System.Drawing.Point(155, 90);
            this.lb_usedSpaceBi.Name = "lb_usedSpaceB" + i;
            this.lb_usedSpaceBi.Size = new System.Drawing.Size(0, 13);
            this.lb_usedSpaceBi.TabIndex = 10;
            // 
            // lb_totSizeGB
            // 
            this.lb_totSizeGBi.AutoSize = true;
            this.lb_totSizeGBi.Location = new System.Drawing.Point(265, 170);
            this.lb_totSizeGBi.Name = "lb_totSizeGB" + i;
            this.lb_totSizeGBi.Size = new System.Drawing.Size(0, 13);
            this.lb_totSizeGBi.TabIndex = 8;
            // 
            // lb_totSizeB
            // 
            this.lb_totSizeBi.AutoSize = true;
            this.lb_totSizeBi.Location = new System.Drawing.Point(155, 170);
            this.lb_totSizeBi.Name = "lb_totSizeKB" + i;
            this.lb_totSizeBi.Size = new System.Drawing.Size(0, 13);
            this.lb_totSizeBi.TabIndex = 7;
            // 
            // lb_Capacity
            // 
            this.lb_Capacityi.AutoSize = true;
            this.lb_Capacityi.Location = new System.Drawing.Point(50, 170);
            this.lb_Capacityi.Name = "lb_Capacity" + i;
            this.lb_Capacityi.Size = new System.Drawing.Size(51, 13);
            this.lb_Capacityi.TabIndex = 6;
            this.lb_Capacityi.Text = Global.GetResourceString("Capacity_Label");
            //
            //   pb_Image
            // 
            this.pb_Imagei.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pb_Imagei.Location = new System.Drawing.Point(70, 203);
            this.pb_Imagei.Name = "pb_Image";
            this.pb_Imagei.Size = new System.Drawing.Size(60, 40);
            this.pb_Imagei.TabIndex = 0;
            this.pb_Imagei.TabStop = false;
            // 
            // DiskDetails
            //           
            this.tb_Disk_Names.ResumeLayout(false);
            this.tabPagei.ResumeLayout(false);
            this.tabPagei.PerformLayout();
            this.ResumeLayout(false);

        }

        private void InitializeComponent(bool state)
        {
            if (state)
            {
                this.bt_close = new System.Windows.Forms.Button();
                this.tb_Disk_Names = new System.Windows.Forms.TabControl();
                this.lb_monitorStatus = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // bt_close
                // 
                this.bt_close.Location = new System.Drawing.Point(284, 354);
                this.bt_close.Name = "bt_close";
                this.bt_close.Size = new System.Drawing.Size(75, 23);
                this.bt_close.TabIndex = 103;
                this.bt_close.Text = Global.GetResourceString("CloseButton_Text");
                this.bt_close.UseVisualStyleBackColor = true;
                this.bt_close.Click += new System.EventHandler(this.bt_close_Click);
                // 
                // tb_Disk_Names
                // 
                this.tb_Disk_Names.Location = new System.Drawing.Point(0, 0);
                this.tb_Disk_Names.Name = "tb_Disk_Names";
                this.tb_Disk_Names.SelectedIndex = 0;
                this.tb_Disk_Names.Size = new System.Drawing.Size(398, 336);
                this.tb_Disk_Names.TabIndex = 104;
                // 
                // lb_monitorStatus
                // 
                this.lb_monitorStatus.Hide();
                // 
                // DiskDetails
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(373, 389);
                this.Controls.Add(this.lb_monitorStatus);
                this.Controls.Add(this.bt_close);
                this.Controls.Add(this.tb_Disk_Names);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "DiskDetails";
                this.ShowInTaskbar = false;
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = Global.GetResourceString("DiskDetails_Title");
                this.ResumeLayout(false);
                this.PerformLayout();
            }
            else
            {
                this.bt_close = new System.Windows.Forms.Button();
                this.lb_monitorStatus = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // bt_close
                // 
                this.bt_close.Location = new System.Drawing.Point(284, 354);
                this.bt_close.Name = "bt_close";
                this.bt_close.Size = new System.Drawing.Size(75, 23);
                this.bt_close.TabIndex = 103;
                this.bt_close.Text = Global.GetResourceString("CloseButton_Text");
                this.bt_close.UseVisualStyleBackColor = true;
                this.bt_close.Click += new System.EventHandler(this.bt_close_Click);
                // 
                // lb_monitorStatus
                // 
                this.lb_monitorStatus.AutoSize = true;
                this.lb_monitorStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.lb_monitorStatus.Location = new System.Drawing.Point(20, 119);
                this.lb_monitorStatus.Name = "lb_monitorStatus";
                this.lb_monitorStatus.Size = new System.Drawing.Size(52, 17);
                this.lb_monitorStatus.TabIndex = 105;
                this.lb_monitorStatus.Text = Global.GetResourceString("Monitor_Status");
                // 
                // DiskDetails
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(373, 389);
                this.Controls.Add(this.lb_monitorStatus);
                this.Controls.Add(this.bt_close);
                this.Controls.Add(this.tb_Disk_Names);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "DiskDetails";
                this.ShowInTaskbar = false;
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = Global.GetResourceString("DiskDetails_Title");
                this.ResumeLayout(false);
                this.PerformLayout();
            }
        }

        #endregion


        /// <summary>
        /// Loads Disk Details Form
        /// </summary>
        /// <param name="visioShape">
        /// Server shape reference whose details are going to be displayed
        /// </param>
        public void DiskDetailsLoad(Visio.Shape visioShape)
        {
            string name = "";
            try
            {
                //Get the Network Name and IP Address property.
                if (visioShape == null)
                {
                    throw new ArgumentNullException(string.Empty);
                }
                name = visioShape.get_Cells("Prop.NetworkName").Formula.ToUpper(CultureInfo.CurrentCulture);

                //Create temp datatable to store monitor disk parameters
                dsDriveDetails = new DataSet();
                dsDriveDetails.Locale = CultureInfo.InvariantCulture;

                dtTempDiskDetails = dsDriveDetails.Tables.Add();
                dtTempDiskDetails.Columns.Add("ServerName", Type.GetType("System.String"));
                dtTempDiskDetails.Columns.Add("DiskName", Type.GetType("System.String"));
                dtTempDiskDetails.Columns.Add("TotalSizeMB", Type.GetType("System.Int32"));
                dtTempDiskDetails.Columns.Add("FreeSpaceMB", Type.GetType("System.Int32"));
                dtTempDiskDetails.Columns.Add("FileSystem", Type.GetType("System.String"));
                dtTempDiskDetails.Columns.Add("Description", Type.GetType("System.String"));

                //Set primary key
                dtTempDiskDetails.PrimaryKey = new DataColumn[] { dtTempDiskDetails.Columns[0], dtTempDiskDetails.Columns[1] };
                               
                //Add values to temp datatable
                CreateTempTable(name);
                
                if (dtTempDiskDetails.Rows.Count != 0)
                {
                    //Initialize form components
                    InitializeComponent(true);

                    //Set server name as form caption
                    this.FindForm().Text = name.Replace("\"", "");

                    //Insert Tabs in Form by no. of drives.
                    InsertTabs(1, dtTempDiskDetails);
                }
                else
                {
                    //Initialize form components
                    InitializeComponent(false);

                    this.FindForm().Text = name.Replace("\"", "");
                }
            }
            catch (NrtmException ex)
            {
                //Exception from sub function. Pass on to entry point
                throw new NrtmException("", ex.InnerException);
            }
            catch (Exception ex)
            {
                //Exception from current function
                throw new NrtmException("", ex);
            }
        }

        ///<summary>
        ///Insert tabs on tabcontrol as per no. of drives 
        ///</summary>
        ///<param name="dtdriveDetails">
        /// Temporary datatable consists of data to be shown on disk details tabs
        ///</param>
        ///<param name="tabNumber">
        /// Drive tab number
        ///</param>
        private void InsertTabs(int tabNumber, DataTable dtdriveDetails)
        {
            try
            {
                //Sort datatable using disk name
                dtdriveDetails.DefaultView.Sort = "[" + dtdriveDetails.Columns[1].ColumnName + "] ASC";

                //Calculate and Display free space, used space, total space, file system, disk type  
                foreach (DataRowView drView in dtdriveDetails.DefaultView)
                {

                    string diskName = drView.Row.ItemArray[1].ToString();

                    long totalSizeMB = Convert.ToInt64(drView.Row.ItemArray[2].ToString(), CultureInfo.InvariantCulture);
                    long freeSpaceMB = Convert.ToInt64(drView.Row.ItemArray[3].ToString(), CultureInfo.InvariantCulture);

                    //File System and Type
                    string fileSystem = drView.Row.ItemArray[4].ToString();
                    string type = drView.Row.ItemArray[5].ToString();

                    //Used Space in Bytes and GB
                    long usedSpaceB = (totalSizeMB - freeSpaceMB) * 1024 * 1024;
                    long usedSpaceGB = (totalSizeMB - freeSpaceMB) / 1024;

                    //Free Space in Bytes and GB
                    long freeSpaceB = (freeSpaceMB * 1024) * 1024;
                    long freeSpaceGB = freeSpaceMB / 1024;

                    //Total Space in Bytes and GB
                    long totSizeB = (totalSizeMB * 1024) * 1024;
                    long totSizeGB = totalSizeMB / 1024;

                    //Initialize form components on the basis of drive tabs
                    InitializeComponent(tabNumber.ToString(CultureInfo.InvariantCulture));
                    
                    this.tabPagei.Text = diskName;
                    this.lb_freeSpaceGBi.Text = Convert.ToString(freeSpaceGB, CultureInfo.InvariantCulture) + Global.GetResourceString("GigaBytes_Text");
                    this.lb_freeSpaceBi.Text = Convert.ToString(freeSpaceB, CultureInfo.InvariantCulture) + Global.GetResourceString("Bytes_Text");
                    this.lb_usedSpaceGBi.Text = Convert.ToString(usedSpaceGB, CultureInfo.InvariantCulture) + Global.GetResourceString("GigaBytes_Text");
                    this.lb_usedSpaceBi.Text = Convert.ToString(usedSpaceB, CultureInfo.InvariantCulture) + Global.GetResourceString("Bytes_Text");
                    this.lb_totSizeGBi.Text = Convert.ToString(totSizeGB, CultureInfo.InvariantCulture) + Global.GetResourceString("GigaBytes_Text");
                    this.lb_totSizeBi.Text = Convert.ToString(totSizeB, CultureInfo.InvariantCulture) + Global.GetResourceString("Bytes_Text");
                    this.lblFSi.Text = fileSystem;
                    this.lblTypei.Text = type;

                    //Draw a pie-chart showing free space and used space
                    Draw(freeSpaceMB, totalSizeMB);

                }
            }
            catch (NrtmException ex)
            {
                //Exception from sub function. Pass on to entry point
                throw new NrtmException("", ex.InnerException);
            }
            catch (Exception ex)
            {
                //Exception from current function
                throw new NrtmException("", ex);
            }
        }
        /// <summary>
        /// Stores data values in a temp datatable i.e. to be displayed on the disk details form 
        /// </summary>
        /// <param name="machineName">
        /// Machine name whose details to be viewed
        /// </param>
        /// <param name="ipAddress"></param>
        private void CreateTempTable(string machineName)
        {
            try
            {
                //Get disk details to store values into temp datatable
                DataTable dtdriveDetails = nrtmDocument.MonitorDB.DtDiskDetails;

                //Used machine name to build temp data table
                if (!string.IsNullOrEmpty(machineName))
                {
                    foreach (DataRow dtRow in dtdriveDetails.Rows)
                    {
                        if (String.Compare(dtRow.ItemArray[0].ToString(), machineName.Replace('"', ' ').Trim(), true, CultureInfo.CurrentCulture) == 0)
                        {
                            try
                            {
                                //Add data to temp table
                                dtTempDiskDetails.ImportRow(dtRow);
                            }
                            catch
                            {
                                //No Catch because of duplicate rows
                            }
                        }
                    }
                }
            }
            catch (NrtmException ex)
            {
                //Exception from sub function. Pass on to entry point
                throw new NrtmException("", ex.InnerException);
            }
            catch (Exception ex)
            {
                //Exception from current function
                throw new NrtmException("", ex);
            }
        }      
        
        /// <summary>
        ///   Draws the pie-chart and set as a picture box image.
        /// </summary>
        /// <param name="freeSpace">
        /// </param>
        /// <param name="totalSpace">
        /// </param>
        public void Draw(long freeSpace, long totalSpace)
        {
            const int width = 300, height = 300;
            Bitmap objBitmap = new Bitmap(width, height);
            Graphics objGraphics = Graphics.FromImage(objBitmap);

            try
            {
                //Draws the pie according to free space, total space
                Draw3DPieChart(ref objGraphics, freeSpace, totalSpace);

                //Set pie to picture box
                this.pb_Imagei.Image = new Bitmap(objBitmap);

                // clean up...
                objGraphics.Dispose();
                objBitmap.Dispose();
            }
            catch (NrtmException ex)
            {
                //Exception from sub function. Pass on to entry point
                throw new NrtmException("", ex.InnerException);
            }
            catch (Exception ex)
            {
                //Exception from current function
                throw new NrtmException("", ex);
            }
        }

        ///<summary>
        /// Draws a 3D pie chart
        ///</summary>
        ///<param name="freeSpace">
        ///</param>
        ///<param name="totalSpace">
        ///</param>
        ///<param name="objGraphics">
        ///</param>
        private static void Draw3DPieChart(ref Graphics objGraphics, long freeSpace, long totalSpace)
        {

            try
            {
                //Calculate used space
                long usedSpace = totalSpace - freeSpace;

                //Calculate used and free angle
                int usedAngle = (int)((usedSpace * 360) / totalSpace);
                int freeAngle = 360 - usedAngle;

                int iLoop;

                // Create location and size  of ellipse.
                int x = 50;
                int y = 20;
                int width = 100;
                int height = 50;


                // Create start and sweep angles.
                int startAngle = 180;
                int sweepAngle = usedAngle;
                int sweepAngleFreeDepth;
                int sweepAngleUsedDepth;
                int sweepAngleUsedNonDepth;

                //Brush object to color the pie
                SolidBrush objBrush = new SolidBrush(Color.Aqua);

                objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

                //if used space is less than 50% of total space
                if (usedAngle <= 180)
                {
                    objBrush.Color = System.Drawing.Color.RoyalBlue;
                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, startAngle, sweepAngle);
                    //Fill ellipse with color for used space  
                    objGraphics.FillPie(objBrush, x, y, width,
                                height, startAngle, sweepAngle);

                    //Calculate angles
                    startAngle = usedAngle + 180;
                    sweepAngle = freeAngle;

                    objBrush.Color = System.Drawing.Color.Fuchsia;

                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, 0,
                            180);
                    //Fill ellipse with color for free space  
                    objGraphics.FillPie(objBrush, x, y,
                                width, height, startAngle, sweepAngle);

                }
                //if used space is less than 75% of total space
                else if (usedAngle <= 270 && usedAngle > 180)
                {
                    objBrush.Color = System.Drawing.Color.RoyalBlue;
                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, startAngle, sweepAngle);

                    //Fill ellipse with color for used space 
                    objGraphics.FillPie(objBrush, x, y, width,
                                height, startAngle, sweepAngle);

                    //Calculate angles
                    startAngle = usedAngle - 180;
                    sweepAngle = freeAngle;

                    objBrush.Color = System.Drawing.Color.Fuchsia;

                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, startAngle,
                            sweepAngle);

                    //Fill ellipse with color for free space
                    objGraphics.FillPie(objBrush, x, y,
                                width, height, startAngle, sweepAngle);

                }
                //if used space is more than 75% of total space
                else if (usedAngle > 270)
                {
                    //Calculate angles
                    startAngle = 180;
                    sweepAngleFreeDepth = freeAngle;

                    objBrush.Color = System.Drawing.Color.Fuchsia;

                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, startAngle, -sweepAngleFreeDepth);

                    //Fill ellipse with color for free space 
                    objGraphics.FillPie(objBrush, x, y, width,
                                height, startAngle, -sweepAngleFreeDepth);

                    //Calculate angles
                    startAngle = 180 - sweepAngleFreeDepth;
                    sweepAngleUsedDepth = usedAngle - 180;
                    sweepAngleUsedNonDepth = 180;

                    objBrush.Color = System.Drawing.Color.RoyalBlue;

                    //Create 3D shadow
                    for (iLoop = 0; iLoop < 10; iLoop++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
                            objBrush.Color), x,
                            y + iLoop, width, height, startAngle, -sweepAngleUsedDepth);

                    //Fill ellipse with color for used space with depth/shadow
                    objGraphics.FillPie(objBrush, x, y,
                                width, height, startAngle, -sweepAngleUsedDepth);

                    //Fill ellipse with color for used space without depth/shadow
                    objGraphics.FillPie(objBrush, x, y,
                                width, height, 0, -sweepAngleUsedNonDepth);
                }
            }
            catch (NrtmException ex)
            {
                //Exception from sub function. Pass on to entry point
                throw new NrtmException("", ex.InnerException);
            }
            catch (Exception ex)
            {
                //Exception from current function
                throw new NrtmException("", ex);
            }            
        }

        /// <summary>
        /// Close button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;            
            return;
        }        
    }
}

